package com.xywh.wanandroid.compose_ui.webView

import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.MutableTransitionState
import androidx.compose.animation.expandVertically
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.shrinkVertically
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableFloatState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import com.xywh.composeLibrary.view.XxTopAppBarCenter

@Composable
fun WebViewScreen(
    title: String,
    link: String,
    transitionState: MutableTransitionState<Boolean>,
    progress: MutableFloatState,
    animatedProgress: Float,
    navigateUp: () -> Unit
) {

    val baseWebChromeClinet: WebChromeClient = object : WebChromeClient() {

        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            super.onProgressChanged(view, newProgress)
            progress.value = (newProgress / 100.0).toFloat()
            if (newProgress == 100) {
                transitionState.targetState = false
            }
        }
    }
    val density = LocalDensity.current
    XxTopAppBarCenter(
        title = title,
        navigationIconOnClick = { navigateUp.invoke() },
        content = { innerPadding ->
            Column(
                Modifier
                    .padding(top = innerPadding.calculateTopPadding())
            ) {

                AnimatedVisibility(
                    visibleState = transitionState,
                    enter = slideInVertically {
                        // Slide in from 40 dp from the top.
                        with(density) { -40.dp.roundToPx() }
                    } + expandVertically(
                        // Expand from the top.
                        expandFrom = Alignment.Top
                    ) + fadeIn(
                        // Fade in with the initial alpha of 0.3f.
                        initialAlpha = 0.3f
                    ),
                    exit = slideOutVertically() + shrinkVertically() + fadeOut()
                ) {
                    LinearProgressIndicator(
                        progress = { animatedProgress },
                        modifier = Modifier.fillMaxWidth(),
                    )
                }
                WebViewAndroidView(baseWebChromeClinet, link)
            }


        })
}

@Composable
private fun WebViewAndroidView(baseWebChromeClinet: WebChromeClient, link: String?) {
    AndroidView(factory = { context ->
        WebView(context).apply {
            webChromeClient = baseWebChromeClinet
            webViewClient = WebViewClient()

            overScrollMode = WebView.OVER_SCROLL_NEVER
            settings.domStorageEnabled = true
            settings.javaScriptEnabled = true
            settings.loadsImagesAutomatically = true
            settings.useWideViewPort = true
            settings.loadWithOverviewMode = true
            settings.layoutAlgorithm =
                WebSettings.LayoutAlgorithm.SINGLE_COLUMN
            settings.mixedContentMode =
                WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
        }

    }, Modifier.fillMaxWidth()) { webView ->
        if (link != null) {
            webView.loadUrl(link)
        }
    }
}
